<template>
	<view class="content_view">
		<u-navbar title-color="#FFFFFF" height="48" back-icon-color="#FFFFFF" :border-bottom="false" :is-fixed="true"  :title="'个人信息'"></u-navbar>
		<u-cell-group >
			<u-cell-item :border-bottom="true" value="" title="头像" :arrow="true" @click="updateAvatar()">
				<u-avatar slot="value" :src="userInfo.avatar" mode="square" ></u-avatar>
			</u-cell-item>
			<u-cell-item :border-bottom="true"  title="昵称" :value="userInfo.nickName" :arrow="true" @click="updateInfo('nickName')"></u-cell-item>
			<u-cell-item :border-bottom="true"  title="姓名" :value="userInfo.name" :arrow="true" @click="updateInfo('name')"></u-cell-item>
			<u-cell-item :border-bottom="true"  title="性别" :value="userInfo.sex|formDateSex" :arrow="true" @click="showSex"></u-cell-item>
			<u-cell-item :border-bottom="true"  title="联系电话" :value="userInfo.mobile|hideCodeFilter" :arrow="true" @click="updateInfo('mobile')"></u-cell-item>
			<u-cell-item :border-bottom="true"  title="电子邮件" :value="userInfo.email" :arrow="true" @click="updateInfo('email')"></u-cell-item>
			<u-cell-item :border-bottom="true"  title="密码" :arrow="true" @click="updateInfo('password')"></u-cell-item>
			<u-cell-item v-if="userInfo.enterpriseName" :border-bottom="false"  title="所属企业" :value="userInfo.enterpriseName" :arrow="false"></u-cell-item>
		</u-cell-group>
		<u-toast ref="uToast"></u-toast>
		<u-select v-model="sexShow" :list="list" :default-value="defaultValue" @cancel="sexShow=false" @confirm="updateSex"></u-select>
	</view>
</template>

<script>
	import store from "../../store/index.js"
	export default {
		data() {
			return {
				userInfo:{},
				shape: 'square',
				list:[
					{
						value: 1,
						label: '男'
					},
					{
						value: 2,
						label: '女'
					}
				],
				sexShow:false,
				defaultValue:[]
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			getUserInfo(){
				this.userInfo = this.$store.getters.userInfo
			},
			updateInfo(val){
				uni.navigateTo({
					url:"/pages/account/update/"+val
				})
			},
			showSex(){
				this.sexShow = true
				this.defaultValue = [this.userInfo.sex-1]
			},
			updateSex(val){
				this.userInfo.sex = val[0].value
				this.utils.request("/api/app/user/updateInUser",{sex:val[0].value},"post","app",true).then(res=>{
					this.$refs.uToast.show({
						title: '性别更新成功',
						type: 'success',
						url: ''
					})
				}).catch(error=>{
					this.$refs.uToast.show({
						title: '性别更新失败',
						type: 'error',
						url: ''
					})
				})
			},
			updateAvatar(){
				console.log(123)
				let _this = this;
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success: function (res) {
						console.log(res)
						_this.utils.uploadFile("/api/app/user/updateAvatar",res.tempFilePaths[0],{type:'avatar'},"app",true).then(res=>{
							_this.userInfo.avatar = res.data
							this.$refs.uToast.show({
								title: '头像更新成功',
								type: 'success',
								url: ''
							})
						}).catch(error=>{
							this.$refs.uToast.show({
								title: '头像更新失败',
								type: 'error',
								url: ''
							})
						})
				    }
				});
			},
			test(){
				console.log(123)
			}
		}
	}
</script>

<style>

</style>
